package com.googlecode.jfxgap;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;

import org.apache.cordova.api.CordovaInterface;

/**
 * Cordova plugin interface for Desktop implementation.
 * @author Cyril Joui
 */
public class DesktopCordovaContext implements CordovaInterface {

	/**
	 * Reference to Desktop Cordova app.
	 */
	private JavaFxCordovaApp javaFxCordovaApp;

	/**
	 * Executor service.
	 */
	private ExecutorService executorService;

	/**
	 * Default constructor.
	 * @param javaFxCordovaApp app reference
	 */
	public DesktopCordovaContext(JavaFxCordovaApp javaFxCordovaApp) {
		this.javaFxCordovaApp = javaFxCordovaApp;
		executorService = Executors.newCachedThreadPool();
	}

	/**
	 * {@inheritDoc}
	 * @see org.apache.cordova.api.CordovaInterface#onMessage(java.lang.String, java.lang.Object)
	 */
	@Override
	public Object onMessage(String id, Object data) {
		if ("splashscreen".equals(id)) {
			if ("hide".equals(data)) {
				javaFxCordovaApp.getSplash().destroy(new EventHandler<ActionEvent>() {
		              @Override public void handle(ActionEvent actionEvent) {
		            	  javaFxCordovaApp.getStage().show();
		                }
		              });				
			}
		}
		return null;
	}

	/**
	 * {@inheritDoc}
	 * @see org.apache.cordova.api.CordovaInterface#getThreadPool()
	 */
	@Override
	public ExecutorService getThreadPool() {
		return executorService;
	}

}
